package com.nearby.android.unitl;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * socket 通讯格式
 * @author lijianwei
 */
public class Message {
	private int command; //命令
	private String fromTo; //发送人
	private String sendTo; //接受人
	private String msgBody; //信息内容
	
	public Message(){
	}
	public Message(int command, String fromTo, String sendTo, String msgBody){
		this.command = command;
		this.fromTo = fromTo;
		this.sendTo = sendTo;
		this.msgBody = msgBody;
	}
	//转换成jsonobject
	public JSONObject Msg2JsonObject() {
		JSONObject obj = new JSONObject();
		try {
			obj.put("command", command);
			obj.put("fromTo", fromTo);
			obj.put("sendTo", sendTo);
			obj.put("msgBody", msgBody);
			
			return obj;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	//转换成string
	public String Msg2String(){
		String str =  Msg2JsonObject().toString();
		return str;
	}
	//string转换成jsonObject
	public static JSONObject Str2JsonObject(String str){
		try {
			return new JSONObject(str);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	//string 转成  Message 对象
	public static Message Str2Message(String str){
		JSONObject obj = Str2JsonObject(str);
		try {
			Message msg = new Message(obj.getInt("command"), obj.getString("fromTo"), obj.getString("sendTo"), obj.getString("msgBody"));
			return msg;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int getCommand() {
		return command;
	}
	public void setCommand(int command) {
		this.command = command;
	}
	public String getFromTo() {
		return fromTo;
	}
	public void setFromTo(String fromTo) {
		this.fromTo = fromTo;
	}
	public String getSendTo() {
		return sendTo;
	}
	public void setSendTo(String sendTo) {
		this.sendTo = sendTo;
	}
	public String getMsgBody() {
		return msgBody;
	}
	public void setMsgBody(String msgBody) {
		this.msgBody = msgBody;
	}
}
